<?php

namespace App\Http\Service;

use App\Model\AuthNode;
use App\Http\Util\Result;

trait ServiceAuthNode{

    /**
     * @return mixed
     * 父节点分页列表
     */
    public static function getList($title,$limit){
        if (empty($title)) {
            $list = AuthNode::where('nid',0)->paginate($limit);
        } else {
            $list = AuthNode::where("title","like", '%'.$title.'%')->where('nid',0)->paginate($limit);
        }
        return Result::success()->setData($list);
    }

    /**
     * @return mixed
     * 父节点列表
     */
    public static function getLists(){
        $res = AuthNode::where('nid',0)->get()->toarray();
        return Result::success()->setData($res);

    }

    /**
     *获取单个id记录
     */
    public static function getOne($id) {
        $res = AuthNode::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新记录
     */
    public static function updateData($input, $id) {
        $sel = AuthNode::where('id','<>',$id)
            ->where('node',$input['node'])
            ->orwhere('title',$input['title'])
            ->count();
        if ($sel) {
            return Result::failure()->setMsg('已存在相同节点或节点名！');
        }
        $res = AuthNode::where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     *删除记录
     */
    public static function deleteNode($id) {
        $res = AuthNode::where("id", $id)->delete();
        return Result::success()->setData($res);
    }

    /**
     * @param $id
     * 禁用、启用
     */
    public static function able($id,$able) {
        $res = AuthNode::where("id", $id)->update(['status' => $able]);
        return Result::success()->setData($res);
    }

    /**
     * 父节点下拉
     */
    public static function nidList() {
        $res = AuthNode::where('nid',0)->select('id', 'title')->get();
        return Result::success()->setData($res);
    }
}


